// pages/regist/index.js
var utl = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ischecked:true,
    msg:'发送验证码',
    showtime:'',
    count:60,
    tel:'',
    valad_code:'',  //验证码
    invite_code:'', //邀请码
    uname:'',//用户姓名
    cantouch:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.jsonStr;
    console.log('转发的id'+id);
    if(id != undefined){
      this.setData({
        invite_code: id
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //注册
  registAction:function(){
    const isc = this.data.ischecked;
    var err = '';
    if(!isc){
      wx.showToast({
        title: '同意服务协议才能注册',
        icon:'none',
        duration:2000,
      })
      return;
    }
    const telp = this.data.tel;
    const tcode = this.data.valad_code;
    const invitecode = this.data.invite_code;
    const uname = this.data.uname;
    if(telp==''){
      err='手机号不为空';
    }else if(tcode==''){
      err='验证码不为空';
    }else if(invitecode ==''){
      err=='邀请码不为空';
    }else if(uname==''){
      err = '姓名不为空';
    }
    if(err!=''){
      wx.showToast({
        title: err,
        icon:'none',
        duration:2000,
      })
      return;
    }
    wx.showLoading({
      title: '请求中...',
    })
    utl.httServer('/epay_apps/regist/submitA0300', { 'recommendUserId': invitecode, 'mobile': telp, 'userName': uname, 'captcha': tcode,'participateActivity':'0','code':'' }, (err, res) => {
     wx.hideLoading();
   
     if(err){
       wx.showToast({
         title: err,
         icon:'none',
         duration:2000,
       })
       return;
     }
     wx.showModal({
       title: '提示',
       content: '注册成功，前往登录界面',
       showCancel:false,
       success(res) {
         if (res.confirm) {
           wx.reLaunch({
             url: '/pages/login/index',
           })
         } else if (res.cancel) {
           console.log('用户点击取消')
         }
       }

     })
    })

  },
  //获取手机号
  telephoneAc:function(e){
    var t = e.detail.value;
    this.setData({
      tel:t,
    })
  },

  vonfire:function(){
    var check = this.data.ischecked;
    check = !check;
    this.setData({
      ischecked:check
    })
  },
  senmessage:function(){
    var tele = this.data.tel;
    if(tele==''){
      return;
    }
    if(!utl.checkPhone(tele)){
      wx.showToast({
        title: '手机号校验有误',
        duration:2000,
        icon:'none'
      })
      return;
    }
    wx.showLoading({
      title: '请求中...',
    })
    utl.httServer('/epay_apps/captcha/sendCodeA0200', { 'mobile': tele,'smsType':'0'},(err,res)=>{
      wx.hideLoading();
      var mess='';
      if(err){
        mess=err;
      }else{
        mess = res.msg;
      }
      wx.showToast({
        title: mess,
        icon: 'none',
        duration: 2000
      })
    })

    this.beginTimer();
  },
  beginTimer:function(){
    var Timers = null;
    this.setData({
      showverification: '',
      count: 60,
      showtime: '60s'
    })
    var that = this;
    //如果定时器已经器动了
    if (!Timers) {
      Timers = setInterval(function () {
        var count = that.data.count - 1;
        that.setData({
          showtime: count + "s",
          Timers: Timers,
          count: count
        })
        if (count <= 0) {
          clearInterval(Timers)
          that.setData({
            count: 60,
            msg: '再次发送',
            showtime: ''
          })
        }
      }, 1000);
    }
  },
  //验证码
  msgvaliad:function(e){
    this.setData({
      valad_code:e.detail.value,
      cantouch: true,
    })
  }, 
  //邀请码
  inviteCode:function(e){
    this.setData({
      invite_code:e.detail.value,
    
    })
  },
  //姓名
  nameAction:function(e){
    this.setData({
      uname: e.detail.value,
    })
  }
})